package com.msf.frametest;

import com.formdev.flatlaf.FlatDarkLaf;
import com.msf.frametest.components.InnerFrame;
import com.msf.frametest.components.ModalDialog;
import com.msf.frametest.components.NonModalDialog;
import com.msf.frametest.utils.UIUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.swing.*;
import java.awt.*;

@SpringBootApplication
public class FrameTestApplication extends JFrame {

    private UIUtil uiUtil = new UIUtil();

    public FrameTestApplication() {
        try {
            UIManager.setLookAndFeel(new FlatDarkLaf());

            initFrame();

            setTitle("模态窗口切换测试");
            setSize(800, 600);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void initFrame() {
        JButton frame1Button = uiUtil.createButton("Frame1窗口", e -> { showFrame1(); });
        JButton frame2Button = uiUtil.createButton("Frame2窗口", e -> { showFrame2(); });
        JButton modalButton = uiUtil.createButton("模态对话框", e -> { showModalDialog(); });
        JButton nonModalButton = uiUtil.createButton("非模态对话框", e -> { showNonModalDialog(); });

        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(frame1Button);
        getContentPane().add(frame2Button);
        getContentPane().add(modalButton);
        getContentPane().add(nonModalButton);
    }

    private void showFrame1() {
        InnerFrame frame = new InnerFrame();
        frame.setTitle("Frame 1");
        frame.setVisible(true);
    }

    private void showFrame2() {
        InnerFrame frame = new InnerFrame();
        frame.setTitle("Frame 2");
        frame.toFront();
        frame.setVisible(true);
    }

    private void showModalDialog() {
        ModalDialog modalDialog = new ModalDialog(this);
        modalDialog.setVisible(true);
    }

    private void showNonModalDialog() {
        NonModalDialog nonModalDialog = new NonModalDialog(this);
        nonModalDialog.setVisible(true);
    }

    public static void main(String[] args) {
        SpringApplication.run(FrameTestApplication.class, args);
    }

}
